<?php 
namespace XFCore\traits\user;
trait PayTrait {
    public function topup_money($g , $post){
        if($this->islogin_user != 1) {
            exitjson(-1, "请先登录");
        }
        unset($post['type']);
        $type = $this->input->type;
        $params = array_filter($post, fn($v) => $v !== '');
        if(empty($params['money']) || !is_numeric($params['money']) || $params['money'] <= 0){
            exitjson(-1,'金额为空或不合法！');
        }
        $typedata = ['alipay','qqpay','wxpay','usdt'];
        if(!in_array($type,$typedata) || empty($type)){
            exitjson(-1,'支付方式不存在！');
        }
        $userrow = $this->db->userrow($this->conf->zero_pid , 'pid' , '`pid`');
        if(empty($userrow)){
            exitjson(-1,'后台未配置充值接口 请联系管理员配置后在试');
        }
        $order = 'Pay'.date("YmdHis").rand(11111,99999);
        
        $data = [
            'pid'=>$this->uid,
            'order'=>$order,
            'beizhu'=>'pay_money'
        ];
        $params = array_merge($params , $data);
        $this->db->insert('insideorder',$params);
        $params['type'] = $type;
        $params['name'] = '站点内部充值余额';
        $params['return_url'] = $this->httphost . "user/Pay_Vip";
        $pay_url = $this->submit_pay($userrow , $params);
        exitjson(200,'发起成功,正在跳转中请稍后....',['pay_url'=>$pay_url]);
        
    }
    
    public function member_renew(){
        if($this->islogin_user != 1) {
            exitjson(-1, "请先登录");
        }
        $pay_type = $this->input->pay_type;
        $type = $this->input->type;
        
        $money = $this->conf[$type . '_free_vip_money'];
        
        if(empty($money) || !is_numeric($money) || $money <= 0){
            exitjson(-1,'金额为空或不合法！');
        }
        $typedata = ['alipay','qqpay','wxpay'];
        if(!in_array($pay_type,$typedata) || empty($pay_type)){
            exitjson(-1,'支付方式不存在！');
        }
        if(!in_array($type,$typedata)|| empty($type)){
            exitjson(-1,'请选择需要充值的通道会员');
        }
        $userrow = $this->db->userrow($this->conf->zero_pid , 'pid' , '`pid`');
        if(empty($userrow)){
            exitjson(-1,'后台未配置充值接口 请联系管理员配置后在试');
        }
        $order = 'Pay'.date("YmdHis").rand(11111,99999);
        $data = [
            'order'=>$order,
            'pid'=>$this->uid,
            'money'=>$money,
            'beizhu'=>'pay_vip|'.$type 
        ];
        $this->db->insert('insideorder',$data);
        $params = [
            'order'=>$order,
            'type'=>$pay_type,
            'money'=>$money,
            'return_url'=>$this->httphost . "user/Pay_Vip",
            'name'=>"开通/续费会员"
        ];
        $pay_url = $this->submit_pay($userrow , $params);
        exitjson(200,'发起成功,正在跳转中请稍后....',['pay_url'=>$pay_url]);
        
        
    }
    public function submitPayment(){
        if($this->islogin_user != 1) {
            exitjson(-1, "请先登录");
        }
        $type = $this->input->type;
        $id   = $this->input->id;
        if(empty($id)){
            exitjson(-1,'请选择要购买的套餐');
        }
        $typedata = ['alipay','qqpay','wxpay','usdt'];
        if(!in_array($type,$typedata)|| empty($type)){
            exitjson(-1,'请选择支付方式');
        }
        $row = $this->db->packagerow($id , 'id','`status`,`id`,`price`');
        if(empty($row) || $row['status'] == 0){
            exitjson(-1,'套餐数据不存在');
        }
        $money = $row['price'];
        if(empty($money) || !is_numeric($money) || $money <= 0){
            exitjson(-1,'金额错误 请联系管理员处理！');
        }
        $userrow = $this->db->userrow($this->conf->zero_pid , 'pid' , '`pid`');
        if(empty($userrow)){
            exitjson(-1,'后台未配置充值接口 请联系管理员配置后在试');
        }
        $order = 'Pay'.date("YmdHis").rand(11111,99999);
        $data = [
            'order'=>$order,
            'pid'=>$this->uid,
            'money'=>$money,
            'beizhu'=>'pay_packages|'.$id 
        ];
        $this->db->insert('insideorder',$data);
        $params = [
            'order'=>$order,
            'type'=>$type,
            'money'=>$money,
            'return_url'=>$this->httphost . "user/packages",
            'name'=>"购买套餐"
        ];
        $pay_url = $this->submit_pay($userrow , $params);
        exitjson(200,'发起成功,正在跳转中请稍后....',['pay_url'=>$pay_url]);
    }
    public function pay_cs($get , $post){
        if($this->islogin_user != 1) {
            exitjson(-1, "请先登录");
        }
        $type  = $this->input->type;
        $money = $this->input->money;
        if(empty($money) || !is_numeric($money) || $money <= 0){
            exitjson(-1,'金额错误 请联系管理员处理！');
        }
        $typedata = ['alipay','qqpay','wxpay','usdt'];
        if(!in_array($type,$typedata)|| empty($type)){
            exitjson(-1,'请选择支付方式');
        }
        $userrow = $this->db->userrow($this->uid , 'pid' , '`pid`');
        if(empty($userrow)){
            exitjson(-1,'账号不存在');
        }
        $order = 'Pay'.date("YmdHis").rand(11111,99999);
        $data = [
            'order'=>$order,
            'pid'=>$this->uid,
            'money'=>$money,
            'beizhu'=>'pay_cs'
        ];
        $this->db->insert('insideorder',$data);
        $params = [
            'order'=>$order,
            'type'=>$type,
            'money'=>$money,
            'return_url'=>$this->httphost . "user/pay_cs",
            'name'=>"支付测试"
        ];
        
        $pay_url = $this->submit_pay($userrow , $params);
        exitjson(200,'发起成功,正在跳转中请稍后....',['pay_url'=>$pay_url]);
    }
    
    public function reg_pay($get , $post){
        $type  = $this->input->type;
        $money = $this->conf->reg_pay_price;
        $token = $this->input->token;
        if(empty($money) || !is_numeric($money) || $money <= 0){
            exitjson(-1,'金额错误 请联系管理员处理！');
        }
        $typedata = ['alipay','qqpay','wxpay','usdt'];
        if(!in_array($type,$typedata)|| empty($type)){
            exitjson(-1,'请选择支付方式');
        }
        $userrow = $this->db->userrow($this->conf->zero_pid , 'pid' , '`pid`');
        if(empty($userrow)){
            exitjson(-1,'后台未配置充值接口 请联系管理员配置后在试');
        }
        $order = 'Pay'.date("YmdHis").rand(11111,99999);
        $data = [
            'order'=>$order,
            'pid'=>$this->conf->zero_pid,
            'money'=>$money,
            'beizhu'=>'reg|'.$token
        ];
        $this->db->insert('insideorder',$data);
        $params = [
            'order'=>$order,
            'type'=>$type,
            'money'=>$money,
            'return_url'=>$this->httphost . "user/login",
            'name'=>"商户注册"
        ];
        $pay_url = $this->submit_pay($userrow , $params);
        exitjson(200,'发起成功,正在跳转中请稍后....',['pay_url'=>$pay_url]);
    }
    protected function submit_pay($userrow , $params){
        $arr = [
            'pid'=>$userrow['pid'],
            'type'=>$params['type'],
            'money'=>$params['money'],
            'trade_no'=>$params['order'],
            'out_trade_no'=>$params['order'],
            'notify_url'=>$this->httphost . "pay/notify/".$params['order'],
            'return_url'=>$params['return_url'],
            'name'=>$params['name'],
            'sign'=>"xfpay123456"
        ];
        
        $api = $this->httphost . 'pay/pay_submit/' . $params['order'];
        $data = [
            'token'=>aes256auth(json_encode($arr) , SYS_KEY)
        ];
        $res = get_curl($api , $data);
        
        $res = json_decode($res,true);
        
        if($res['code'] == 200){
            return $res['pay_url'];
        }else{
            exitjson(-1,$res['msg']);
        }
    }
    
    
    
}
?>